package com.example.mediamoudle.pickvideo;

import android.content.Context;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.util.Log;
import android.view.View;

import com.android.baselibrary.base.BaseAdapter;
import com.bumptech.glide.Glide;
import com.example.mediamoudle.R;
import com.example.mediamoudle.databinding.ShortvideoVideoPickerItemVideoPickBinding;


import com.example.mediamoudle.utlis.Util;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class PickVideoAdapter extends BaseAdapter<VideoFile, ShortvideoVideoPickerItemVideoPickBinding> {


    private int mCurrentNumber = 0;
    public String mVideoPath;
    protected OnSelectStateListener mListener;

/*

    @Override
    public VideoPickViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(, parent, false);
        ViewGroup.LayoutParams params = itemView.getLayoutParams();
        if (params != null) {
            WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
            int width = wm.getDefaultDisplay().getWidth();
            params.height = width / PickVideoActivity.COLUMN_NUMBER;
        }
        return new VideoPickViewHolder(itemView);
    }
*/



    /**
     * 判断视频是否有效视频  录制过短 是无效视频
     *
     * @auth sxy
     * @date 2022/12/23 13:15
     */
    public boolean isHasVideo(Context context, String videoPath) {
        boolean isVideo = false;
        try {
            MediaMetadataRetriever retriever = new MediaMetadataRetriever();
            retriever.setDataSource(context, Uri.parse(videoPath));
            String hasVideo = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO);
            isVideo = "yes".equals(hasVideo);
        }catch (Exception e){
            e.printStackTrace();
        }

        return isVideo;
    }

    @Override
    public int getItemLayoutId() {
        return R.layout.shortvideo_video_picker_item_video_pick;
    }

    @Override
    public void covert(@NotNull ShortvideoVideoPickerItemVideoPickBinding binding, VideoFile videoFile, int position) {
        binding.ivCamera.setVisibility(View.INVISIBLE);
        binding.ivThumbnail.setVisibility(View.VISIBLE);
        binding.cbx.setVisibility(View.VISIBLE);
        binding.layoutDuration.setVisibility(View.VISIBLE);

         VideoFile file = videoFile;



        if (position==0){
            Log.i("TAGsa", "onBindViewHolder: "+file.isSelected());
        }
        Glide.with(binding.getRoot().getContext())
                .load(file.getPath())
                .into(binding.ivThumbnail);

        if (file.isSelected()) {
            binding.cbx.setSelected(true);
            binding.shadow.setVisibility(View.VISIBLE);
        } else {
            binding.cbx.setSelected(false);
            binding.shadow.setVisibility(View.INVISIBLE);
        }

        binding.cbx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               /* if (!isHasVideo(v.getContext(),file.getPath())){
                    ToastUtils.showCenterToast(mContext,R.string.shortvideo_void_video);
                    return;
                }
                if (!v.isSelected() && isUpToMax()) {
                    ToastUtils.showCenterToast(mContext,R.string.shortvideo_picker_max_video_limit);
                    return;
                }
                MediaExtractor extractor = new MediaExtractor();
                try {
                    extractor.setDataSource(file.getPath());
                    if (extractor.getTrackCount()<=1){
                        ToastUtils.showCenterToast(mContext,R.string.shortvideo_void_video);
                        extractor.release();
                        return;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (file.getDuration()> ShortVideoHelper.PICKER_SELECT_MAX_TIME+999) {  //处理毫秒值超过限制秒数问题
                    *//**
                     * 只有超过最大限制的时候才会展示提示
                     *//*
                    ToastUtils.showCenterToast(
                            mContext,
                            R.string.shortvideo_picker_max_duration_limit
                    );
                    return;
                }*/
              //  int index = isNeedCamera ? holder.getAdapterPosition() - 1 : holder.getAdapterPosition();
                if (v.isSelected()) {
                    binding.shadow.setVisibility(View.INVISIBLE);
                    binding.cbx.setSelected(false);
                    mCurrentNumber--;
                } else {
                    binding.shadow.setVisibility(View.VISIBLE);
                    binding.cbx.setSelected(true);
                    mCurrentNumber++;
                }


                videoFile.setSelected(binding.cbx.isSelected());
                if (mListener != null) {
                    mListener.OnSelectStateChanged(binding.cbx.isSelected(), videoFile);
                }
            }
        });

       /* binding.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int index = isNeedCamera ? holder.getAdapterPosition() - 1 : holder.getAdapterPosition();
                if (onPreviewListener != null) {
                    onPreviewListener.onPreview(mList.get(index), mList,index);
                }
            }
        });*/

       binding.txtDuration.setText(Util.getDurationString(file.getDuration()));
    }






    public boolean isUpToMax() {
        return mCurrentNumber >= 9;
    }

    public void setCurrentNumber(int number) {
        mCurrentNumber = number;
    }

    public OnPreviewListener onPreviewListener;

    public void setOnPreviewListener(OnPreviewListener onPreviewListener) {
        this.onPreviewListener = onPreviewListener;
    }

    public interface OnPreviewListener {
        void onPreview(VideoFile videoFile, ArrayList<VideoFile> mList,int position);
    }
    public ArrayList<VideoFile> getList(){
        return (ArrayList<VideoFile>) getMData();
    }


    public interface OnSelectStateListener {
        void OnSelectStateChanged(boolean state, VideoFile file);
    }
    public void setOnSelectStateListener(OnSelectStateListener listener) {
        mListener = listener;
    }

}
